package org.jsola.hr.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISalaryCycleDAO;
import org.jsola.hr.dto.SalaryCycleAddDTO;
import org.jsola.hr.dto.SalaryCycleUpdateDTO;
import org.jsola.hr.entity.SalaryCycleDO;
import org.jsola.hr.query.SalaryCycleQuery;
import org.jsola.hr.service.ISalaryCycleService;
import org.jsola.hr.vo.SalaryCycleListVO;
import org.jsola.hr.vo.SalaryCycleVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrSalaryCycleServiceImpl")
public class SalaryCycleServiceImpl implements ISalaryCycleService {

    @Autowired
    private ISalaryCycleDAO salaryCycleDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SalaryCycleVO save(SalaryCycleAddDTO salaryCycleAddDTO, TokenUser tokenUser) {
        // 转DO
        SalaryCycleDO salaryCycleDO = salaryCycleAddDTO.to(SalaryCycleDO.class);
        // 保存
        salaryCycleDO = save(salaryCycleDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return salaryCycleDO.to(SalaryCycleVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SalaryCycleUpdateDTO salaryCycleUpdateDTO, TokenUser tokenUser) {
        //转DO
        SalaryCycleDO salaryCycleDO = salaryCycleUpdateDTO.to(SalaryCycleDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(salaryCycleDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... salaryCycleIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) salaryCycleIds);
    }


    @Override
    public SalaryCycleVO selectById(String salaryCycleId, String siteId) {
        SalaryCycleDO salaryCycleDO = selectDOById(salaryCycleId, siteId);
        if (salaryCycleDO == null) {
            return null;
        }
        return salaryCycleDO.to(SalaryCycleVO.class);
    }

    @Override
    public List<SalaryCycleListVO> select(SalaryCycleQuery salaryCycleQuery, String siteId) {
        List<SalaryCycleDO> salaryCycleDOList = selectDO(salaryCycleQuery, siteId);
        if(CollectionUtils.isEmpty(salaryCycleDOList)) {
            return salaryCycleDOList == null ? null : new ArrayList<>();
        }
        return salaryCycleDOList.stream()
                .map(salaryCycleDO -> salaryCycleDO.to(SalaryCycleListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SalaryCycleQuery salaryCycleQuery, String siteId) {
        Example example = buildExample(salaryCycleQuery,siteId);
        return salaryCycleDAO.selectCountByExample(example);
    }

    @Override
    public Page<SalaryCycleListVO> selectPage(SalaryCycleQuery salaryCycleQuery, String siteId) {
        Example example = buildExample(salaryCycleQuery,siteId);
        Page<SalaryCycleDO> page = salaryCycleDAO.selectPageByExample(example,
                salaryCycleQuery.getPageNo(),
                salaryCycleQuery.getPageSize());

        return page.to(SalaryCycleListVO.class);
    }

    @Override
    public SalaryCycleDO selectDOById(String salaryCycleId, String siteId) {
        return listById(salaryCycleId, siteId);
    }

    @Override
    public List<SalaryCycleDO> selectDO(SalaryCycleQuery salaryCycleQuery, String siteId) {
        Example example = buildExample(salaryCycleQuery,siteId);
        return salaryCycleDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param salaryCycleQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SalaryCycleQuery salaryCycleQuery, String siteId) {
        Example example = new Example(SalaryCycleDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (salaryCycleQuery.getPayMonth() != null) {
            example.and().andEqualTo("payMonth", salaryCycleQuery.getPayMonth());
        }
        if (salaryCycleQuery.getPayDate() != null) {
            example.and().andEqualTo("payDate", salaryCycleQuery.getPayDate());
        }
        if (salaryCycleQuery.getSocMonth() != null) {
            example.and().andEqualTo("socMonth", salaryCycleQuery.getSocMonth());
        }
        if (salaryCycleQuery.getSocDataSource() != null) {
            example.and().andEqualTo("socDataSource", salaryCycleQuery.getSocDataSource());
        }
        if (salaryCycleQuery.getAttendanceDataSource() != null) {
            example.and().andEqualTo("attendanceDataSource", salaryCycleQuery.getAttendanceDataSource());
        }
        if (salaryCycleQuery.getAttendanceModuleItem() != null) {
            example.and().andEqualTo("attendanceModuleItem", salaryCycleQuery.getAttendanceModuleItem());
        }
        if (salaryCycleQuery.getLeaveDownsizing() != null) {
            example.and().andEqualTo("leaveDownsizing", salaryCycleQuery.getLeaveDownsizing());
        }
        if (salaryCycleQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", salaryCycleQuery.getCompanyId());
        }
        if (salaryCycleQuery.getGmtCreateStart() != null &&
                salaryCycleQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", salaryCycleQuery.getGmtCreateStart(),
                    salaryCycleQuery.getGmtCreateEnd());
        }
        if (salaryCycleQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + salaryCycleQuery.getCreateUserId() + "%");
        }
        if (salaryCycleQuery.getGmtModifiedStart() != null &&
                salaryCycleQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", salaryCycleQuery.getGmtModifiedStart(),
                    salaryCycleQuery.getGmtModifiedEnd());
        }
        if (salaryCycleQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + salaryCycleQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,salaryCycleQuery.getOrders());
        return example;
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SalaryCycleVO saveOrUpdate(SalaryCycleUpdateDTO salaryCycleUpdateDTO, TokenUser tokenUser){
        SalaryCycleDO salaryCycleDO = salaryCycleUpdateDTO.to(SalaryCycleDO.class);
        SalaryCycleDO salaryCycleDOTmp = selectDOBySiteId(tokenUser.getSiteId());

        if(StringUtils.isBlank(salaryCycleUpdateDTO.getId())){
            //添加判断是否存在
            if(Objects.nonNull(salaryCycleDOTmp)){
                return salaryCycleDOTmp.to(SalaryCycleVO.class);
            }
            // 保存
            salaryCycleDO = save(salaryCycleDO, tokenUser.getSiteId(), tokenUser.getUserId());
            return salaryCycleDO.to(SalaryCycleVO.class);
        }else{
            //编辑判断是否存在
            if(Objects.isNull(salaryCycleDOTmp)){
                return null;
            }
            //根据主键更新，只更新非null值
            updateByIdSelective(salaryCycleDO, tokenUser.getSiteId(), tokenUser.getUserId());
            return salaryCycleDO.to(SalaryCycleVO.class);
        }
    }

    @Override
    public SalaryCycleDO selectDOBySiteId(String siteId) {
        Example example = new Example(SalaryCycleDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        return salaryCycleDAO.selectOneByExample(example);
    }
}




